package com.rxb.blog.controller;

import com.rxb.blog.common.CommonResult;
import com.rxb.blog.entity.UserEntity;
import com.rxb.blog.service.UserService;
import com.rxb.blog.utils.BlogException;
import com.rxb.blog.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * Author：rxb
 * Date：2020-01-16 16:48
 * Description：<描述>
 */
@Api(description = "用户操作接口")
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "通过id获取用户信息", notes="通过id获取用户信息")
    @ApiImplicitParam(name = "id", value = "用户id", paramType = "query", required = true, dataType = "Integer")
    @GetMapping("getUser/{id}")
    public CommonResult<UserEntity> GetUser(@PathVariable int id) {
        return CommonResult.success(userService.selectById(id));
    }

    //用户登录
    @ApiOperation(value = "用户登录", notes="用户登录")
    @ApiImplicitParam(name = "userVo", value = "用户实体类vo", paramType = "body", required = true, dataType = "UserVo")
    @PostMapping("login")
    public CommonResult<String> login(@RequestBody UserVo userVo, HttpServletRequest request) {
        try {
            userService.login(userVo,request);

            HttpSession session = request.getSession();
            session.setAttribute("username",userVo.getUsername());

            return CommonResult.success("登录成功");
        } catch (BlogException e) {
            return CommonResult.failed(e.getMessage());
        }
    }

    //用户退出
    @ApiOperation(value = "用户退出", notes="用户退出")
    @ApiImplicitParam(name = "userVo", value = "用户实体类vo", paramType = "body", required = true, dataType = "UserVo")
    @PostMapping("loginOut")
    public CommonResult<String> loginOut( HttpServletRequest request){
        HttpSession session = request.getSession();
        session.removeAttribute("username");
        session.invalidate();
        return CommonResult.success("退出成功");
    }


    //用户注册
    @ApiOperation(value = "用户注册", notes="用户注册")
    @ApiImplicitParam(name = "userVo", value = "用户实体类vo", paramType = "body", required = true, dataType = "UserVo")
    @PostMapping("register")
    public CommonResult<String> userRegister(@RequestBody UserVo userVo, HttpServletRequest request) {
        try {
            String registMessage = userService.register(userVo);
            return CommonResult.success(registMessage);
        }catch (BlogException e){
            return CommonResult.failed(e.getMessage());
        }
    }

    //发送验证码
    @ApiOperation(value = "发送验证码", notes="发送验证码")
    @ApiImplicitParam(name = "userVo", value = "用户实体类vo", paramType = "body", required = true, dataType = "UserVo")
    @PostMapping("sendIdentifyCode")
    public CommonResult<String> sendIdentifyCode(@RequestBody UserVo userVo){
        try {
            userService.sendIdentifyCode(userVo);
            return  CommonResult.success();
        } catch (IOException e) {
            e.printStackTrace();
            return CommonResult.failed(e.getMessage());
        } catch (MessagingException e) {
            e.printStackTrace();
            return CommonResult.failed(e.getMessage());
        }catch (BlogException e){
            e.printStackTrace();
            return CommonResult.failed(e.getMessage());
        }
    }

    //找回密码-发送验证码
    @ApiOperation(value = "找回密码_密码发送至邮箱", notes="找回密码_密码发送至邮箱")
    @ApiImplicitParam(name = "userVo", value = "用户实体类vo", paramType = "body", required = true, dataType = "UserVo")
    @PostMapping("findPassword")
    public CommonResult<String> findPassword(@RequestBody UserVo userVo){
        try {
            userService.findPassword(userVo);
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return CommonResult.failed(e.getMessage());
        } catch (MessagingException e) {
            e.printStackTrace();
            return CommonResult.failed(e.getMessage());
        }catch (BlogException e){
            e.printStackTrace();
            return CommonResult.failed(e.getMessage());
        }
    }

    @ApiOperation(value = "分页查询用户信息", notes="分页查询用户信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "currentPage", value = "当前页", paramType = "query", required = true, dataType = "Integer"),
                        @ApiImplicitParam(name = "pageSize", value = "每页数据量", paramType = "query", required = true, dataType = "Integer")})
    @GetMapping("getUsers/{currentPage}/{pageSize}")
    public CommonResult<Map> getUsers(@PathVariable int currentPage, @PathVariable int pageSize){
        return CommonResult.success(userService.getUsers(currentPage,pageSize));
    }

    @ApiOperation(value = "更新用户信息",notes = "更新用户信息")
    @ApiImplicitParam(name = "userVo", value = "用户实体类vo", paramType = "body", required = true, dataType = "UserVo")
    @PostMapping("updateUser")
    public void updateUser(@RequestBody UserVo userVo){
        userService.updateUser(userVo);
    }

    @ApiOperation(value = "根据id删除用户",notes ="根据id删除用户")
    @ApiImplicitParam(name = "userVo", value = "用户实体类vo", paramType = "body", required = true, dataType = "UserVo")
    @PostMapping("deleteUser")
    public void deleteUserById(@RequestBody UserVo userVo){
        userService.deleteUser(userVo);
    }

    @ApiOperation(value = "添加用户",notes ="添加用户")
    @ApiImplicitParam(name = "userVo", value = "用户实体类vo", paramType = "body", required = true, dataType = "UserVo")
    @PostMapping("addUser")
    public CommonResult<String> addUser(@RequestBody UserVo userVo){
        try {
            userService.addUser(userVo);
            return CommonResult.success();
        }catch (BlogException e){
            return CommonResult.failed(e.getMessage());
        }
    }

    @ApiOperation(value = "批量删除用户",notes ="批量删除用户")
    @PostMapping("deleteUsers")
    public void deleteUsers(@RequestBody List<Integer> ids){
        userService.deleteUsers(ids);
    }
}
